package models

import (
	"fmt"
	"regexp"
)

//匹配导演名称
func GetMovieDirector(moviehtml string) string {
	if moviehtml == "" {
		return ""
	}

	reg := regexp.MustCompile(`<a.*?rel="v:directedBy">(.*)</a>`)

	result := reg.FindAllStringSubmatch(moviehtml, -1)
	if len(result) == 0 {
		return ""
	}
	return string(result[0][1])

}

//匹配电影名
func GetPianNmae(htmls string) string {
	if htmls == "" {
		return ""
	}
	//regexp是正则的包，我们先写好正则的规则
	reg := regexp.MustCompile(`<span\s*property="v:itemreviewed">(.*)</span>`)
	//然后进行匹配 -1 表示全部返回 如果写一个1 他就返回匹配到的第一个  返回是一个[][]string
	result := reg.FindAllStringSubmatch(htmls, -1)
	//如果没有匹配到内容返回空
	if len(result) == 0 {
		return ""
	}
	return string(result[0][1])
}

//匹配编剧
func GetBianju(moviehtml string) string {
	if moviehtml == "" {
		return ""
	}
	reg := regexp.MustCompile(`<a\s*href="/celebrity/\d+/">(.*?)</a>`)
	result := reg.FindAllStringSubmatch(moviehtml, -1)
	if len(result) == 0 {
		return ""
	}
	res := ""

	for _, v := range result {
		if res == "" {
			res += v[1]
		} else {
			res = res + "," + v[1]
		}
	}

	return res
}

//匹配主演
func GetZhuyan(moviehtml string) string {
	if moviehtml == "" {
		return ""
	}
	reg := regexp.MustCompile(`<a.*?rel="v:starring">(.*?)</a>`)
	result := reg.FindAllStringSubmatch(moviehtml, -1)

	if len(result) == 0 {
		return ""
	}

	res := ""

	for _, v := range result {
		if res == "" {
			res += v[1]
		} else {
			res = res + "," + v[1]
		}
	}
	return res
}

//电影类型
func Dianyleix(moviehtml string) string {
	if moviehtml == "" {
		return ""
	}

	reg := regexp.MustCompile(`<span\s*property="v:genre">(.*?)</span>`)
	result := reg.FindAllStringSubmatch(moviehtml, -1)

	if len(result) == 0 {
		return ""
	}

	res := ""

	for _, v := range result {
		if res == "" {
			res += v[1]
		} else {
			res = res + "," + v[1]
		}

	}
	return res
}

//图片地址
func GetPicsrc(moviehtml string) string {
	if moviehtml == "" {
		return ""
	}
	reg := regexp.MustCompile(`<img\s*src="(.*?)"\s*title="点击看更多海报"\s*alt=".*"\s*rel="v:image"\s*/>`)

	result := reg.FindAllStringSubmatch(moviehtml, -1)
	fmt.Println(result)

	if len(result) == 0 {
		return ""
	}

	return string(result[0][1])
}

//制作国家
func Getage(moviehtml string) string {
	if moviehtml == "" {
		return ""
	}
	reg := regexp.MustCompile(`<span\s*class="pl">制片国家/地区:</span>\s*(.*?)\s*<br/>`)

	result := reg.FindAllStringSubmatch(moviehtml, -1)
	if len(result) == 0 {
		return ""
	}

	return string(result[0][1])
}

//电影语言
func GetLang(moviehtml string) string {
	if moviehtml == "" {
		return ""
	}
	reg := regexp.MustCompile(`<span\s*class="pl">语言:</span>\s*(.*?)<br/>`)

	result := reg.FindAllStringSubmatch(moviehtml, -1)
	if len(result) == 0 {
		return ""
	}

	return string(result[0][1])
}

//电影上映时间
func GetShangtime(moviehtml string) string {
	if moviehtml == "" {
		return ""
	}
	reg := regexp.MustCompile(`<span\s*class="pl">上映日期:</span>\s*<span\s*property="v:initialReleaseDate"\s*content="(.*?)">`)

	result := reg.FindAllStringSubmatch(moviehtml, -1)
	if len(result) == 0 {
		return ""
	}

	return string(result[0][1])
}

//电影时长
func GetTime(moviehtml string) string {
	if moviehtml == "" {
		return ""
	}
	reg := regexp.MustCompile(`<span\s*class="pl">片长:</span>\s*<span\s*property="v:runtime"\s*content="(.*?)">`)

	result := reg.FindAllStringSubmatch(moviehtml, -1)
	if len(result) == 0 {
		return ""
	}

	return string(result[0][1])
}

//电影评分
func Getsping(moviehtml string) string {
	if moviehtml == "" {
		return ""
	}
	reg := regexp.MustCompile(`<strong\s*class="ll\s*rating_num"\s*property="v:average">(.*?)</strong>`)

	result := reg.FindAllStringSubmatch(moviehtml, -1)
	if len(result) == 0 {
		return ""
	}

	return string(result[0][1])
}
